//
//  LoadingView.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/1.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "LoadingView.h"
#import "ZZZWaitingView.h"
@interface LoadingView()
@end

@implementation LoadingView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
       
    }
    return self;
}

+ (LoadingView *) shareLoading{
    static LoadingView *loadingview;
    if (loadingview == nil) {
        
        NSArray *colors = @[MainColor];
        loadingview = [[LoadingView alloc] initWithFrame:CGRectMake((WindowWidth-26)/2, (WindowHeight-26)/2, 26, 26) andLineWidth:3.0 andLineColor:colors];
    }
    return loadingview;
}


- (instancetype)initWithFrame:(CGRect)frame andLineWidth:(CGFloat)lineWidth andLineColor:(NSArray *)lineColor {
    self = [super initWithFrame:frame];
    if(self) {
     

    }
    
    return self;
}

- (void) startAnimating{
    [self stopAnimating];

    __block NSInteger t = 1.2;
    NSTimeInterval period = 0.1; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
        if (t -- <= 0 ) {
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [ZZZWaitingView showWaitViewInView:self];
  
            });
        }
    });
    dispatch_resume(_timer);
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}

- (void) stopAnimating{
    [self removeFromSuperview];
}

@end
